package com.diankangwo.controller;


import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.LogisticsConfigurationDAO;
import com.diankangwo.domain.LogisticsConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 物流控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "物流")
public class LogisticsConfigurationController {
    @Autowired
    private LogisticsConfigurationDAO lcDao;

    /**
     * 按厂家/商家ID、发货地址、收货地址 获取物流信息
     *
     * @param cid              厂家/商家ID
     * @param beForeProvinceId 发货省ID
     * @param beForeCityId     发货市ID
     * @param beForeDistrictId 发货区域ID
     * @param afterProvinceId  收货省ID
     * @param afterCityId      收货市ID
     * @param afterDistrictId  收货区域ID
     * @return
     */
    @ApiOperation(value = "按厂家/商家ID、发货地址、收货地址 获取物流信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "厂家/商家ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "beForeProvinceId", value = "发货省ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "beForeCityId", value = "发货市ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "beForeDistrictId", value = "发货区域ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "afterProvinceId", value = "收货省ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "afterCityId", value = "收货市ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "afterDistrictId", value = "收货区域ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/lc/list", method = RequestMethod.POST)
    public String list(Integer cid, Integer beForeProvinceId, Integer beForeCityId, Integer beForeDistrictId, Integer afterProvinceId, Integer afterCityId, Integer afterDistrictId) {
        String code = "2";
        String msg = "处理异常！";
        LogisticsConfiguration lc = lcDao.findByAddress(cid, beForeProvinceId, beForeCityId, beForeProvinceId, afterProvinceId, afterCityId, afterDistrictId);
        if (lc != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到运费信息！";
        }
        return resultMsg.mapToJson(code, msg, lc);
    }

    /**
     * 按厂家/商家ID 获取物流信息
     *
     * @param cid 厂家/商家ID
     * @return
     */
    @ApiOperation(value = "按厂家/商家ID、 获取物流信息", notes = "")
    @ApiImplicitParam(name = "cid", value = "厂家/商家ID", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/lc/listById", method = RequestMethod.GET)
    public String listById(Integer cid) {
        String code = "2";
        String msg = "处理异常！";
        List<LogisticsConfiguration> lcList = lcDao.findByCID(cid);
        if (lcList != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到运费信息！";
        }
        return resultMsg.mapToJson(code, msg, lcList);
    }

    /**
     * 按ID 删除物流信息
     *
     * @param id 厂家/商家ID
     * @return
     */
    @ApiOperation(value = "删除物流信息", notes = "")
    @ApiImplicitParam(name = "id", value = "数据ID", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/lc/list", method = RequestMethod.GET)
    public String del(Integer id) {
        String code = "2";
        String msg = "处理异常！";
        try {
            lcDao.deleteById(id);
            code = "1";
            msg = "删除成功！";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "系统异常，删除失败！！";
        }
        return resultMsg.mapToJson(code, msg, null
        );
    }

    /**
     * 增加物流信息
     *
     * @param lc 物流对象
     * @return
     */
    @ApiOperation(value = "增加物流信息", notes = "")
    @ApiImplicitParam(name = "lc", value = "物流对象", required = true, dataType = "LogisticsConfiguration")
    @ResponseBody
    @RequestMapping(value = "/lc/insert", method = RequestMethod.GET)
    public String insert(LogisticsConfiguration lc) {
        String code = "2";
        String msg = "处理异常！";
        lc = lcDao.insert(lc);
        if (lc != null) {
            code = "1";
            msg = "查询成功！";
        }else{
            msg = "未查询到运费信息！";
        }
        return resultMsg.mapToJson(code, msg, lc);
    }
}
